<?php
/*
 * 公共添加|删除|更新|查询|类
 */

namespace App\Common;

use App\Common\json;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener;

class data{

    /*
     * $type           类型选择
     * $data           传入数据
     * $message        消息提示[0]为正确消信息，[1]为错误消信提示
     */
    //json 显示文件
    public static function opinion($type,$data,$message = array()){
        switch ($type){
            case "1":
                if($data == true){
                    return json::json('success',$message[0],$data);
                }else{
                    return json::json('failure',$message[1],'');
                }
                break;
            case "2":   //插入操作专用
                if($data == true){
                    return json::json('success',$message[0],json::value(1));
                }else{
                    return json::json('failure',$message[1],json::value(1));
                }
                break;
            case "3":
                if($data == true){
                    DB::commit();
                    return json::json('success',$message[0],'');
                }else{
                    DB::rollback();
                    return json::json('failure',$message[1],'');
                }

                break;
            default:
                break;
        }

    }

    //基础删除
    /*
     * $type          //删除类型
     * $table        //选择表
     * $id           //传参配配
     * $update       //软删除参数配置
     * $parameter    //批量删除配置字典
     * $message        消息提示[0]为正确消信息，[1]为错误消信提示
     */
    public static function del($type,$table,$id = array(),$update = array(),$parameter =null,$message = array()){
        switch ($type){
            case "1":      //真实删除操作一条
                $data      = DB::table($table) ->where($id)->delete();
                self::opinion(1,$data,$message);
                break;
            case "2":      //软删除 更新操作 操作一条
                $data      = DB::table($table) ->where($id) ->update($update);
                self::opinion(1,$data,$message);
                break;
            case "3":      //批量删除
                $arr        = explode(',',$id);
                if(count($arr) ==1){
                    $data     = DB::table($table)->where($parameter,$arr[0]);
                    self::opinion(1,$data,$message);
                }else{
                    DB::beginTransaction();
                    foreach ($id as $key){
                        $data   = DB::table($table) ->where($parameter,$key)->delete();
                    }
                    self::opinion(2,$data,$message);
                }
            case "4":    //批量更新
                $arr        = explode(',',$id);
                if(count($arr) ==1){
                    $data     = DB::table($table)->where($parameter,$arr[0]);
                    self::opinion(1,$data,$message);
                }else{
                    DB::beginTransaction();
                    foreach ($id as $key){
                        $data   = DB::table($table) ->where($parameter,$key)->update($update);
                    }
                    self::opinion(2,$data,$message);
                }
            default:
                break;
        }
    }

    /*
     * $table               //表名
     * $where               //更新条件 array('name'=>1,'pass'=>2)
     * $update              //更新参数array('name'=>1,'pass'=>2)
     * $message        消息提示[0]为正确消信息，[1]为错误消信提示
     */
    //基础更新操作
    public static function updata($table,$where = array(),$update = array(),$message = array()){
        $data      = DB::table($table)
                   ->where($where)
                   ->update($update);
      return  self::opinion(2,$data,$message);
    }


    //基础插入操作
    /*
     * $table               //表名
     * $insert              插入参数  array('name'=>1,'pass'=>1)
     * $message        消息提示[0]为正确消信息，[1]为错误消信提示
     */
    public static function insert($table,$insert = array(),$message =array()){
        $select       = implode(',',array_keys($insert));
        $data         = DB::table($table)
                      ->select($select)
                      ->insert($insert);
      return  self::opinion(2,$data,$message);
    }

    //基本查询
    /*
     * $type      //类型选择
     * $table     //表名
     * $by        //排序 array('id','ASC ')
     * $select    //选用字典 array('name','pass');
     * $where     //条件参数 array('name'=>1,'pass'=>2);
     */
    public static function select($type,$table,$by = array(),$select = array(),$where = array()){
        switch ($type){
            case "1":     //获取全部数据 不建议使用，请用第二种
                $data      = DB::table($table) ->orderBy($by[0],$by[1])->get();
                return $data;
                break;
            case "2":    //获取全部自己想要字典的数据
                $data     = DB::table($table) ->select($select) ->orderBy($by[0],$by[1]) ->get();
                return   $data;
            case "3":     //获取一条数据
                $data      = DB::table($table) ->select($select) ->where($where)->first();
                return    $data;
                break;
            case "4":    //按条件获取数据
                $data     = DB::table($table)  ->select($select) ->where($where) ->orderBy($by[0],$by[1])->get();
                return $data;
                break;
            default:
                break;
        }

    }

    //聚合处理
    public static function group($type,$table,$value){
        switch ($type){
            case "1":     //全表统计
                $data = DB::table('users')->count();
                return $data;
                break;
            case "2":      //按要求统计
                $data  = DB::table($table) ->count($value);
            return $data;
            break;
            case "3" :   //最大值
                $data = DB::table($table)->max($value);
                return $data;
                break;
            case "4":    //最小值
                $data  = DB::table($table) ->min($value);
                return $data;
                break;
            case "5":
            default:
                break;
        }
    }



}
